import axios from "../plugin/http";

export default {
  getPermissions(payload, cb, errorCb) {
    axios
      .get(`/api/permission/${payload.sysId}/`)
      .then(result => {
        cb(result.data);
      })
      .catch(err => {
        errorCb(err);
      });
  },
  getPermissionsWithRole(payload, cb, errorCb) {
    axios
      .get(`/api/authorization/role/${payload.sysId}/${payload.roleId}`)
      .then(result => {
        cb(result.data);
      })
      .catch(err => {
        errorCb(err);
      });
  },
  updatePermission(payload, cb, errorCb) {
    // debugger;
    axios
      .put(`/api/permission/${payload.sysId}/${payload.permissionId}`, {
        "header": {
          "sysCode": "IDS",
          "platform": window.navigator.platform,
          "timestamp": new Date().getTime()
        },
        "payload": {
          "permissionDescription": payload.permissionDescription,
          "permissionUrl": payload.permissionUrl,
          "permissionType": payload.permissionType
        }
      })
      .then(result => {
        cb(result.data);
      })
      .catch(err => {
        errorCb(err);
      });
  },
  insertPermission(payload, cb, errorCb) {
    axios
      .post(`/api/permission/${payload.sysId}/`, {
        "header": {
          "sysCode": "IDS",
          "platform": window.navigator.platform,
          "timestamp": new Date().getTime()
        },
        "payload": {
          "permissionDescription": payload.permissionDescription,
          "permissionUrl": payload.permissionUrl,
          "permissionType": payload.permissionType
        }
      })
      .then(result => {
        cb(result.data);
      })
      .catch(err => {
        errorCb(err);
      });
  },
  deletePermission(payload, cb, errorCb) {
    axios
      .delete(`/api/permission/${payload.sysId}/${payload.permissionId}`, {
        "header": {
          "sysCode": "IDS",
          "platform": window.navigator.platform,
          "timestamp": new Date().getTime()
        },
      })
      .then(result => {
        cb(result.data);
      })
      .catch(err => {
        errorCb(err);
      });
  },
  updatePermissionsWithRole(payload, cb, errorCb) {
    axios
      .put(`/api/authorization/role/${payload.sysId}/${payload.roleId}`, {
        "header": {
          "sysCode": "IDS",
          "platform": window.navigator.platform,
          "timestamp": new Date().getTime()
        },
        "payload": {
          "roleCode": payload.roleCode,
          "permissions": payload.permissions
        }
      })
      .then(result => {
        cb(result.data);
      })
      .catch(err => {
        errorCb(err);
      });
  },
}
